<template>
  <div class="visitor-container">
    <div class="top-item-group">
      <el-select v-model="serviceValue" clearable placeholder="全部客服">
        <el-option
          v-for="item in serviceOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="sourceValue" clearable placeholder="全部来源">
        <el-option
          v-for="item in sourceOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="terminalValue" clearable placeholder="全部终端">
        <el-option
          v-for="item in terminalOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-date-picker
        v-model="value1"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-input placeholder="请输入关键词搜索" v-model="searchKeyword" clearable></el-input>
      <el-button type="primary">搜索</el-button>
      <el-button style="float:right">导出数据</el-button>
    </div>
    <div class="middle-table-gorup">
      <el-table
        :header-cell-style="{'background-color':'#e6f1ff','height':'62px'}"
        :data="tableData"
        style="width: 100%"
        fit
        v-loading="isLoading"
        @selection-change="handleSelectionChange"
        :cell-style="cellStyle"
      >
        <el-table-column prop="visitorName" label="访客名称" align="center"></el-table-column>
        <el-table-column prop="device" label="来源" align="center"></el-table-column>
        <el-table-column prop="terminal" label="终端" align="center"></el-table-column>
        <el-table-column prop="visitTime" label="访问时间" align="center"></el-table-column>
        <el-table-column prop="visitDuration" label="访问时长" align="center"></el-table-column>
        <el-table-column prop="ip" label="IP地址" align="center"></el-table-column>
        <el-table-column prop="state" label="访问状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.state=='在线'" style="color:#2ed477">{{scope.row.state}}</span>
            <span v-else style="color: #ff6262">{{scope.row.state}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="createSession(scope.$index, tableData)"
              type="text"
              size="normal"
            >发起会话</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom-pagination-group">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :total="totalData"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import api from "@/api/api";
export default {
  name: "Visitor",
  components: {},
  props: {},
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalData: 0,
      tableData: [],
      serviceOptions: [
        {
          value: "选项1",
          label: "王美美"
        },
        {
          value: "选项2",
          label: "七尾"
        },
        {
          value: "选项3",
          label: "李想"
        },
        {
          value: "选项4",
          label: "王志"
        }
      ],
      serviceValue: "",
      sourceOptions: [
        {
          value: "Mac",
          label: "Mac"
        },
        {
          value: "Windows",
          label: "Windows"
        },
        {
          value: "手机",
          label: "手机"
        }
      ],
      sourceValue: "",
      terminalOptions: [
        {
          value: "chorme",
          label: "chorme"
        },
        {
          value: "APP",
          label: "APP"
        },
        {
          value: "小程序",
          label: "小程序"
        }
      ],
      terminalValue: "",
      searchKeyword: ""
    };
  },
  watch: {
    terminalValue(data) {
      // console.log(data);
    }
  },
  computed: {},
  methods: {
    cellStyle: function(row, column, rowIndex, columnIndex) {
      if (row.column.label === "访问状态") {
        return "color:#3cd681";
      } else {
        return "";
      }
    },
    createSession(index, tableData) {
      // alert(index);
      this.$router.push("/session");
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getPageData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getPageData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getPageData() {
      this.isLoading = true;
      api
        .getAllVisitorAPI({
          page: this.currentPage,
          limit: this.pageSize
        })
        .then(res => {
          this.isLoading = false;
          let { error, data } = res.data;
          if (error) {
            this.$message.error("请求接口异常");
          } else {
            this.totalData = data.total;
            this.tableData = data.page;
          }
        });
    }
  },
  created() {
    this.getPageData();
  },
  mounted() {}
};
</script>

<style scoped>
.top-item-group {
  padding-bottom: 20px;
}
.visitor-container {
  margin: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
}
.middle-table-gorup {
  margin-top: 20px;
  width: 100%;
  display: inline-block;
}
.bottom-pagination-group {
  padding: 20px 20px 0;
  float: right;
}
.el-select {
  width: 162px;
}
.el-input {
  width: 200px;
}
</style>
